package com.ruoyi.common.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author TsingYu
 * @version 1.0
 * @date 2025/8/18 16:21
 */
public class QBTUtil {

    // static  final String CY_GET_DATA_URL =  "192.168.8.251:8086/qbt/api/553036673260679053754558";
    // static  final String CY_GET_DATA_URL =  "http://qbttmeter.com:8086/qbt/api/ydfsc";
     static  final String CY_GET_DATA_URL =  "http://qbttmeter.com:8086/qbt/api/GDQTHJ";
    // static  final String CY_GET_DATA_URL =  "http://qbttmeter.com:8086/qbt/api/jsssswyxgs";
    static  final String USER_NO =  "GDQTHJ";
    static  final String USER_PW =  "GDqt147!#$";
    static  final String MY_KEY =  "553036673260679053754558";

    public static Map<String,  String> GetSignData() {
        Map<String, String> result  = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        try {
        String data =  USER_NO + "-" + USER_PW + "-" + System.currentTimeMillis() ;
        params.put("data",DesUtil.encodeByECBToBase64( MY_KEY , data));
                // .replace("+","%2B"));

        String  responeseData  = HttpUtil.post(CY_GET_DATA_URL, params, 30 * 1000);
        System.out.println("===============================================data===============" + responeseData);
        if(StringUtils.isBlank(responeseData)){
            return  result;
        }
        JSONObject responeseDataJSON = JSON.parseObject(responeseData);
        if(responeseDataJSON.getInteger("err_no").intValue() ==  0){
            String  dataResp  = responeseDataJSON.getString("data");
            String  keyAndId = DesUtil.decodeByECBToString(MY_KEY, dataResp);
            String[] keyAndIdArr = keyAndId.split("-");
            result.put("key",keyAndIdArr[0]);
            result.put("id",keyAndIdArr[1]);
            return result;
         }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    // public static void main(String[] args) {
    //     //  JSONObject jsonObject = JSON.parseObject(v());
    //     //
    //     //
    //     // String  data  = jsonObject.getString("data");
    //     // String  data  = "/gkUL8W3mIFyHhCBHnrQ+vrjZ5ZMTEv7EISxVzBAbkktDaSOA+0eVg==";
    //     // try {
    //     //     System.out.println(DesUtil.decodeByECBToString(MY_KEY, data));
    //     // } catch (Exception e) {
    //     //     throw new RuntimeException(e);
    //     // }
    //
    //     System.out.println(GetSignData());
    //
    // }

    public static void main(String[] args) {
        Map<String,  String> paramsSign = null;
        Map<String,  String>   paramsRequeest = null;
        HttpUtils httpUtils = new HttpUtils();
        String historyJson = null;
        paramsRequeest = new HashMap<>(8);
        paramsSign = QBTUtil.GetSignData();
        if(paramsSign == null || paramsSign.isEmpty() ){
             return ;
        }
        paramsRequeest.putAll(paramsSign);
        try {
            historyJson = httpUtils.post("http://qbttmeter.com:8086/qbt/api/findUserAndMeterReadingData",
                    new ObjectMapper().writeValueAsString(paramsRequeest));
            System.out.println("===============================================data===============" + historyJson);
        }
        catch(Exception e){
             }
        }


     }



